#include "EpollServer.hpp"
#include <vector>
#include <thread>
#include <memory>

protocol_ns::Response Calculate(const protocol_ns::Request &req)
{
    // 走到这里，一定保证req是有具体数据的！
    // _result(result), _code(code)
    protocol_ns::Response resp(0, 0);
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._result = req._x * req._y;
        break;
    case '/':
        if (req._y == 0)
            resp._code = 1;
        else
            resp._result = req._x / req._y;
        break;
    case '%':
        if (req._y == 0)
            resp._code = 2;
        else
            resp._result = req._x % req._y;
        break;
    default:
        resp._code = 3;
        break;
    }

    return resp;
}

int main()
{
    blockqueue<int>* bq = new blockqueue<int>();
    auto server = [&](IFLISTEN LISTEN)
    {
        std::unique_ptr<EpollServer> server(new EpollServer(Calculate, bq, LISTEN));
        server->InitServer();
        server->Start();
    };

    std::vector<std::thread> ths(3);
    ths[0] = std::thread(server, ISLISTEN);
    for(int i = 1; i < 3; i++)
        ths[i] = std::thread(server, NONELISTEN);
    
    for(int i = 0; i < 3; i++)
        ths[i].join();
    
    delete bq;
    return 0;
}